package com.ysy.tz.mfg.component;

import com.ysy.tz.core.engine.ApplicationContext;
import com.ysy.tz.core.engine.CustomerFXMLLoader;
import com.ysy.tz.core.selfcomp.CommonDialog;
import com.ysy.tz.core.utils.FileResourceLoader;
import com.ysy.tz.core.utils.SessionUtils;
import com.ysy.tz.mfg.entity.UserEntity;
import java.io.IOException;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.geometry.Side;
import javafx.scene.control.Button;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.Dialog;
import javafx.scene.control.Label;
import javafx.scene.control.MenuItem;
import javafx.scene.effect.DropShadow;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;

public class FuncBanner extends StackPane {
    @FXML
    private Button redirectHome;
    @FXML
    private Label funcLabel;
    @FXML
    private ImageView avatar;
    @FXML
    private Label currentUserLabel;
    @FXML
    private Label currentUserArrowBtn;
    @FXML
    private Label flightsLabel;

    public FuncBanner() {
        CustomerFXMLLoader loader = new CustomerFXMLLoader();

        try {
            loader.setRoot(this);
            loader.setController(this);
            loader.setResources(ApplicationContext.commonBundle);
            loader.load(FileResourceLoader.loadInputStream("/views/FuncBannerView.fxml"));
            this.avatar.setImage(new Image(FileResourceLoader.loadInputStream("/views/images/man.png")));
            ImageView imageView = new ImageView(new Image(FileResourceLoader.loadInputStream("/views/images/redirectHome2.png")));
            imageView.setFitHeight(28.0);
            imageView.setFitWidth(28.0);
            this.redirectHome.setGraphic(imageView);
        } catch (IOException var5) {
            throw new RuntimeException(var5);
        }

        this.setStyle("-fx-background-color: blue; -fx-background-insets: 3 0 3 0; -fx-background-radius: 0; ");
        this.setEffect(new DropShadow(0.0, Color.BLACK));
        ContextMenu contextMenu = new ContextMenu();
        MenuItem menuItem2 = new MenuItem(ApplicationContext.commonBundle.getString("changePwd"));
        MenuItem menuItem3 = new MenuItem(ApplicationContext.commonBundle.getString("exit"));
        contextMenu.getItems().add(menuItem2);
        contextMenu.getItems().add(menuItem3);
        menuItem2.setOnAction((action) -> {
            UserEntity userEntity = (UserEntity)SessionUtils.getAttribute(SessionUtils.CURRENT_USER);
            Dialog dialog = CommonDialog.loadFXMLDialog(ApplicationContext.commonBundle.getString("common_changePwd"), "UserChangePwd", "User", new Object[]{userEntity});
            dialog.showAndWait();
        });
        menuItem3.setOnAction((action) -> {
            System.exit(0);
        });
        this.currentUserLabel.setOnMousePressed((mouseEvent) -> {
            contextMenu.show(this.currentUserLabel, Side.BOTTOM, 10.0, 5.0);
        });
        this.currentUserArrowBtn.setOnMousePressed((mouseEvent) -> {
            contextMenu.show(this.currentUserLabel, Side.BOTTOM, 10.0, 5.0);
        });
        this.redirectHome.setOnMousePressed(new EventHandler<MouseEvent>() {
            public void handle(MouseEvent event) {
                ApplicationContext.mainPage.redirectCenterPage();
                FuncBanner.this.setFuncLabel(ApplicationContext.commonBundle.getString("sysMenu"));
            }
        });
    }

    public void setFuncLabel(String funcName) {
        this.funcLabel.setText(funcName);
    }

    public void setCurrentUserLabel(String userName) {
        this.currentUserLabel.setText(userName);
    }

    public void setFlightsText(String flights) {
        this.flightsLabel.setText(flights);
    }

    public void setRedirectShow(boolean isShow) {
        this.redirectHome.setVisible(isShow);
    }
}
